<template>
  <div class="config-group">
    <h4>画布配置</h4>
    
    <!-- 背景相关配置 -->
    <div class="config-section">
      <div class="section-title">背景设置</div>
      
      <!-- 背景图片配置 -->
      <div class="bg-image-config">
        <div class="preview-area">
          <div class="preview-box" :style="previewStyle">
            <span v-if="!config.backgroundImage">暂无图片</span>
          </div>
        </div>
        <div class="image-controls">
          <div class="image-path">
            <span class="path-label">已选图片：</span>
            <span class="path-value">{{ getImageName(config.backgroundImage) || '未选择' }}</span>
          </div>
          <div class="image-actions">
            <ImageSelector 
              :value="config.backgroundImage"
              @input="updateConfig('backgroundImage', $event)"
            />
          </div>
          <div class="image-tips">
            <span class="tips-icon">💡</span>
            <span>图片位于 assets/images 目录下</span>
          </div>
        </div>
      </div>

      <!-- 背景颜色配置 -->
      <div class="config-item">
        <label>背景颜色：</label>
        <el-color-picker
          v-model="config.backgroundColor"
          :predefine="predefineColors"
          show-alpha
          size="small"
          @change="updateConfig('backgroundColor', $event)"
        />
      </div>

      <!-- 网格显示配置 -->
      <div class="config-item">
        <label>网格显示：</label>
        <el-switch
          v-model="config.gridVisible"
          @change="updateConfig('gridVisible', $event)"
        />
      </div>

      <!-- 添加下载按钮 -->
      <div class="config-item">
        <button class="download-btn" @click="downloadCanvas">
          <span class="btn-icon">📥</span>
          下载画布图片
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import ImageSelector from '../../common/ImageSelector.vue'
import { ElColorPicker, ElSwitch } from 'element-plus'

export default {
  name: 'CanvasConfig',
  components: {
    ImageSelector,
    ElColorPicker,
    ElSwitch
  },
  data() {
    return {
      predefineColors: [
        '#2b83b7',
        '#ffffff',
        '#000000',
        '#409EFF',
        '#67C23A',
        '#E6A23C',
        '#F56C6C',
      ]
    }
  },
  props: {
    config: {
      type: Object,
      required: true
    }
  },
  methods: {
    updateConfig(key, value) {
      this.$emit('update:config', {
        ...this.config,
        [key]: value
      })
    },
    getImageName(path) {
      if (!path) return '';
      return path.split('/').pop();
    },
    downloadCanvas() {
      this.$emit('download-canvas');
    }
  },
  computed: {
    previewStyle() {
      return {
        backgroundImage: this.config.backgroundImage ? `url(${this.config.backgroundImage})` : 'none',
        backgroundColor: this.config.backgroundImage ? 'transparent' : '#f5f5f5'
      }
    }
  }
}
</script>

<style scoped>
.config-group {
  margin-bottom: 20px;
  padding: 15px;
  background: #f9f9f9;
  border-radius: 6px;
}

.config-group h4 {
  margin: 0 0 15px 0;
  color: #333;
  font-size: 14px;
  border-bottom: 1px solid #eee;
  padding-bottom: 10px;
}

.config-section {
  margin-bottom: 20px;
  padding: 15px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.config-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 10px;
}

.config-item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.config-item.flex-item {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 180px;
  flex: 1;
}

.config-item label {
  min-width: 70px;
  color: #666;
  flex-shrink: 0;
  font-size: 13px;
}

.config-item input:not([type="color"]),
.config-item select {
  flex: 1;
  padding: 6px 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  min-width: 80px;
  width: 100%;
  font-size: 13px;
}

.config-item.flex-item input:not([type="color"]),
.config-item.flex-item select {
  flex: 1;
  min-width: 80px;
  padding: 4px 8px;
  height: 32px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 13px;
}

/* Element Plus 颜色选择器样式覆盖 */
.config-item :deep(.el-color-picker__trigger) {
  border: 1px solid #ddd;
}

.config-item.flex-item :deep(.el-color-picker) {
  margin-left: 0;
}

.config-item.flex-item :deep(.el-color-picker__trigger) {
  height: 32px;
  width: 60px;
  border: 1px solid #ddd;
  padding: 2px;
}

/* 响应式布局 */
@media screen and (max-width: 1200px) {
  .config-item.flex-item {
    min-width: calc(33.33% - 10px);
  }
}

@media screen and (max-width: 768px) {
  .config-item.flex-item {
    min-width: calc(50% - 10px);
  }
}

/* 添加背景图片相关样式 */
.bg-image-config {
  margin-bottom: 15px;
  background: #f9f9f9;
  border-radius: 4px;
  padding: 12px;
}

.preview-area {
  margin-bottom: 12px;
}

.preview-box {
  width: 100%;
  height: 120px;
  border: 2px dashed #ddd;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  color: #999;
  font-size: 13px;
}

.image-controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.image-path {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.path-label {
  color: #666;
  white-space: nowrap;
}

.path-value {
  color: #1890ff;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.image-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.image-tips {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #999;
  font-size: 12px;
}

.tips-icon {
  font-size: 14px;
}

.section-title {
  font-size: 13px;
  color: #333;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px dashed #eee;
}

/* ImageSelector 组件样式覆盖 */
:deep(.scan-btn) {
  height: 32px;
  padding: 0 12px;
  background: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

:deep(.scan-btn:hover) {
  background: #40a9ff;
}

.download-btn {
  width: 100%;
  padding: 12px;
  background: #1890ff;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-top: 16px;
  transition: all 0.3s ease;
}

.download-btn:hover {
  background: #40a9ff;
}

.btn-icon {
  font-size: 16px;
}
</style> 